package com.training.bt.service;

import com.training.bt.entity.Promotion;
import com.training.bt.mapper.PromotionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PromotionService implements IPromotionService {
    @Autowired
    private PromotionMapper promotionMapper;

    @Override
    public Promotion createPromotion(Promotion promotion) {
        promotionMapper.insert(promotion);
        return promotion;
    }

    @Override
    public Promotion getPromotionById( Long promotionId) {
        return promotionMapper.selectById(promotionId);
    }

    @Override
    public List<Promotion> getAllPromotions() {
        return promotionMapper.selectList(null);
    }

    @Override
    public List<Promotion> getActivePromotions() {
        return promotionMapper.selectByIsActiveTrue();
    }

    @Override
    public List<Promotion> getInProgressPromotions() {
        LocalDateTime now = LocalDateTime.now();
        return promotionMapper.selectByIsActiveTrueAndStartTimeBeforeAndEndTimeAfter(now);
    }

    @Override
    public Promotion updatePromotion(Promotion promotion) {
        promotionMapper.updateById(promotion);
        return promotion;
    }

    @Override
    public void deletePromotion( Long promotionId) {
        promotionMapper.deleteById(promotionId);
    }
    @Override
    public Promotion updatePromotionStatus(Long promotionId, Boolean isActive) {
        Promotion promotion = promotionMapper.selectById(promotionId);
        if (promotion != null) {
            promotion.setIsActive(isActive);
            promotionMapper.updateById(promotion);
        }
        return promotion;
    }
}